package com.haoxin.ai_course.modules.course.biz;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haoxin.ai_course.common.utils.HttpJsonResult;
import com.haoxin.ai_course.modules.course.entity.AcCourse;
import com.haoxin.ai_course.modules.course.entity.AcScore;
import com.haoxin.ai_course.modules.course.entity.AcStatisticsUser;
import com.haoxin.ai_course.modules.course.entity.PO.AcScorePO;
import com.haoxin.ai_course.modules.course.entity.VO.AcScoreVO;
import com.haoxin.ai_course.modules.course.service.AcCourseService;
import com.haoxin.ai_course.modules.course.service.AcScoreService;
import com.haoxin.ai_course.modules.course.service.AcStatisticsUserService;

import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;

/**
 * 答题结果表
 * 
 * @author xingxiangguo
 * @email bugcn@qq.com
 * @date 2022-06-06 09:44:49
 */
@Slf4j
@Component
@Service
public class AcScoreBusiness {
	@Autowired
	AcScoreService acScoreService;
	@Autowired
	private MapperFacade mapperFacade;
	@Autowired
	AcStatisticsUserService acStatisticsUserService;
	@Autowired
	AcCourseService acCourseService;
	public HttpJsonResult<AcScoreVO> save(@RequestBody AcScorePO acScorePO,Long userId,String mobile ) {
		log.info("提交评分记录，用户:{},手机号：{},入参:{}",userId,mobile,acScorePO);
		HttpJsonResult<AcScoreVO> result = new HttpJsonResult<AcScoreVO>();
		QueryWrapper<AcScore> qw = new QueryWrapper<AcScore>();
		qw.lambda().eq(AcScore::getUserId, userId);
		qw.lambda().eq(AcScore::getScoreType, acScorePO.getScoreType());
		qw.lambda().eq(AcScore::getCourseId,acScorePO.getCourseId());
		AcScore acScore = acScoreService.getOne(qw);
		QueryWrapper<AcStatisticsUser> acStatisticsUserQueryWrapper = new QueryWrapper<AcStatisticsUser>();
		acStatisticsUserQueryWrapper.lambda().eq(AcStatisticsUser::getUserId, userId);
		acStatisticsUserQueryWrapper.lambda().eq(AcStatisticsUser::getCourseId, acScorePO.getCourseId());
		acStatisticsUserQueryWrapper.lambda().eq(AcStatisticsUser::getScoreStatisticsFinish, 0);
		acStatisticsUserQueryWrapper.lambda().eq(AcStatisticsUser::getRevisitTimes, 0);
		AcStatisticsUser acStatisticsUser = acStatisticsUserService.getOne(acStatisticsUserQueryWrapper);
		AcScoreVO acScoreVO = mapperFacade.map(acScorePO, AcScoreVO.class);
		AcCourse acCourse = acCourseService.getById( acScorePO.getCourseId());
		if(acStatisticsUser == null) {
			acStatisticsUser = new AcStatisticsUser();
			acStatisticsUser.setCourseId(acScorePO.getCourseId());
			acStatisticsUser.setUserId(userId);
			acStatisticsUser.setRevisitTimes(0);
			acStatisticsUser.setMobile(mobile);
			acStatisticsUser.setCourseCode(acCourse.getCourseCode());
		}
		if (acScore == null) {
			acScore = mapperFacade.map(acScorePO, AcScore.class);
			acScore.setUserId(userId);
		}
		 acScoreVO = mapperFacade.map(acScorePO, AcScoreVO.class);
		boolean updateResult = acScoreService.saveOrUpdate(acScore);
		log.info("提交评分记录评分保存结果：{}",updateResult);
		acScoreVO.setMobile(mobile);
//		评分类型 1、作业汇总星得分 2知识技能星得分 3 愉悦进度得分 4专注进度得分 5 信心进度得分
		switch ( acScorePO.getScoreType()) {
		case 1:
			acStatisticsUser.setHomeworkScore(acScorePO.getScoreValue());
			break;
		case 2:
			acStatisticsUser.setKnowledgeSummaryScore(acScorePO.getScoreValue());
			break;
		case 3:
			acStatisticsUser.setPleasureScore(acScorePO.getScoreValue());
			break;
			
		case 4:
			acStatisticsUser.setEngagementScore(acScorePO.getScoreValue());
			break;
		case 5:
			acStatisticsUser.setConfidenceScore(acScorePO.getScoreValue());
			break;
		default:
			break;
		}
		if(acStatisticsUser.getHomeworkScore()!=null && acStatisticsUser.getKnowledgeSummaryScore()!=null
				&& acStatisticsUser.getPleasureScore()!=null&&acStatisticsUser.getEngagementScore()!=null
				&&acStatisticsUser.getConfidenceScore()!=null) {
			acStatisticsUser.setScoreStatisticsFinish(1);
		}
		log.info("获取课程统计用户信息修改后:{}", acStatisticsUser);
		acStatisticsUserService.saveOrUpdate(acStatisticsUser);
		return result;
	}
	public HttpJsonResult<AcScoreVO> getScoreInfo(Long userId,Integer courseId,Integer scoreType) {
		log.info("获取评分信息，课程id：{}评分类型：{}",courseId,scoreType);
		HttpJsonResult<AcScoreVO> result  = new 	HttpJsonResult<AcScoreVO> ();
		QueryWrapper<AcStatisticsUser> acStatisticsUserQueryWrapper = new QueryWrapper<AcStatisticsUser>();
		acStatisticsUserQueryWrapper.lambda().eq(AcStatisticsUser::getUserId, userId);
		acStatisticsUserQueryWrapper.lambda().eq(AcStatisticsUser::getCourseId, courseId);
		acStatisticsUserQueryWrapper.lambda().eq(AcStatisticsUser::getRevisitTimes, 0);
		AcScoreVO acScoreVO = new AcScoreVO();
		acScoreVO.setScoreType(scoreType);
		acScoreVO.setCourseId(courseId);
		acScoreVO.setCreateBy(""+userId);
		List<AcStatisticsUser> acStatisticsUsers = acStatisticsUserService.list(acStatisticsUserQueryWrapper);
		if(CollectionUtils.isEmpty(acStatisticsUsers) ) {
			acScoreVO.setScoreValue(null);
			result.setData(acScoreVO);
			log.info("获取评分记录完成，用户已经进行购评分，不能再评分返回结果：{}",acScoreVO);
			return result;
		}
		AcStatisticsUser acStatisticsUser = acStatisticsUsers.get(0);
//		acScoreVO.setScoreTitle(null);
		if(acStatisticsUser!=null) {
			switch ( scoreType) {
			case 1:
				acScoreVO.setScoreValue(acStatisticsUser.getHomeworkScore());
				break;
			case 2:
				acScoreVO.setScoreValue(acStatisticsUser.getKnowledgeSummaryScore());
				break;
			case 3:
				acScoreVO.setScoreValue(acStatisticsUser.getPleasureScore());
				break;
				
			case 4:
				acScoreVO.setScoreValue(acStatisticsUser.getEngagementScore());
				break;
			case 5:
				acScoreVO.setScoreValue(acStatisticsUser.getConfidenceScore());
				break;
			default:
				break;
			}
		}
		
		log.info("获取评分记录完成，返回结果：{}",acScoreVO);
		result.setData(acScoreVO);
		return result;
	}
}
